// pages/cart/cart.js
var app = getApp();
var x_start, x;
var userId;
var url = app.globalData.url;

function makeid() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (var i = 0; i < 10; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    return text;
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        URL: app.globalData.url,
        check_all: 0,
        total_price: 0,
        goods: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      userId = wx.getStorageSync('user_id');
      wx.setNavigationBarTitle({
        title: '购物车'
      })
      // console.log(getCurrentPages());
      // var that = this;
      // wx.request({
      //   url: url +'/api/cart/header_cart_list',
      //   method:'POST',
      //   data:{
      //     user_id: userId,
      //   },
      //   success:function(res){
      //     console.log(res);
      //     that.setData({
      //       goods: res.data.cartList,
      //       total_price: res.data.cart_total_price.total_fee
      //     });
      //   }
      // })
    },
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function (options) {
      console.log(userId);
      var that = this;
      wx.request({
        url: url + '/api/cart/header_cart_list',
        method: 'POST',
        data: {
          user_id: userId,
        },
        success: function (res) {
          // console.log(res);
          that.setData({
            goods: res.data.cartList,
            total_price: res.data.total_price.total_fee,
            check_all: res.data.check_all,
          });
        }
      })
    },
    /**
     * 调用全局返回函数 
     */
    returnPage: function(e) {
          app.returnPage(e);
    },
    /**
     * 
     */
    slidestart: function(e) {

        x_start = e.touches[0].clientX
        x = x_start
    },
    /**
     * 滑动
     */
    slide: function(e) {
        var that = this;
        // console.log(e)
        if (e.touches[0].clientX < x) {
            that.data.goods[e.currentTarget.dataset.index].position = -60;
        } else {
            that.data.goods[e.currentTarget.dataset.index].position = 0;
        }
        that.setData({
            goods: that.data.goods
        })
    },
    editGoods:function(){
       var that =this;
       if (e.touches[0].clientX < x) {
         that.data.goods[e.currentTarget.dataset.index].position = -60;
       } else {
         that.data.goods[e.currentTarget.dataset.index].position = 0;
       }
       that.setData({
         goods: that.data.goods
       })
    },
    /**
     * 商品选中事件
     */
    select: function(e) {
      console.log(e.currentTarget.dataset.cartid);
        var that = this;
        // console.log(that.data)
        if (that.data.goods[e.currentTarget.dataset.id].selected == 1) {
            that.data.goods[e.currentTarget.dataset.id].selected = 0;
            wx.request({
              url: url + '/api/cart/edit_cart_selected',//修改选中状态
              method:'POST',
              data:{
                user_id:userId,
                cart_id:e.currentTarget.dataset.cartid,
                selected:0,
              },
              success:function(res){
                console.log(res);
              }
            })
        } else {
            that.data.goods[e.currentTarget.dataset.id].selected = 1;
            wx.request({
              url: url + '/api/cart/edit_cart_selected',//修改选中状态
              method: 'POST',
              data: {
                user_id: userId,
                cart_id: e.currentTarget.dataset.cartid,
                selected: 1,
              },
              success: function (res) {
                console.log(res);
              }
            })
        }
        that.setData({
            check_all: 1,
            total_price: 0
        })
        for (var i in that.data.goods) {
            if (that.data.goods[i].selected == 0) {
                that.setData({
                    check_all: 0
                })
            } else {
                that.setData({
                  total_price: that.data.total_price += that.data.goods[i].goods_fee
                })
            }
        }
        that.setData({
            goods: that.data.goods
        })
    },
    /**
     * 全选事件
     */
    check_all: function(e) {
        var that = this;
        console.log(that.data.goods);
        that.setData({
            total_price: 0
        })
        if (e.currentTarget.dataset.select == 1) {
            that.setData({
                check_all: 0
            })
            for (var i in that.data.goods) {
                that.data.goods[i].selected = 0
            }
            for (var i in that.data.goods) {
              wx.request({
                url: url + '/api/cart/edit_cart_selected',//修改选中状态
                method: 'POST',
                data: {
                  user_id: userId,
                  cart_id: that.data.goods[i].id,
                  selected: 0,
                },
                success: function (res) {
                  console.log(res);
                }
              })
            }
        } else {
            that.setData({
                check_all: 1
            })
            for (var i in that.data.goods) {
                that.data.goods[i].selected = 1
            }
            for (var i in that.data.goods) {
              wx.request({
                url: url + '/api/cart/edit_cart_selected',//修改选中状态
                method: 'POST',
                data: {
                  user_id: userId,
                  cart_id: that.data.goods[i].id,
                  selected: 1,
                },
                success: function (res) {
                  console.log(res);
                }
              })
            }
        }
        for (var i in that.data.goods) {
            if (that.data.goods[i].selected == 1) {
                that.setData({
                  total_price: that.data.total_price += that.data.goods[i].goods_fee
                })
            }
        }
        that.setData({
            goods: that.data.goods
        })
    },
    /**
     * 调用全局返回函数 
     */
    returnPage: function(e) {
        app.returnPage(e);
    },
    submit_order:function(){
      wx.request({
        url: url + '/api/cart/shop_cart_second',//添加订单
        method:"POST",
        data:{
          user_id:userId,
        },
        success:function(res){
          console.log(res);
          var obj = JSON.stringify(res.data);
          wx.navigateTo({
            url: '/pages/order/order?data=' + obj,
          })
        }
      })
    }
})